package com.sskj.linechat.widght;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import com.sskj.linechat.utils.UIUtil;

import java.util.HashMap;

public class TimeRateView extends View {

    //方块的画笔，字体的画笔
    private Paint mPaint, mPaintText, mPaintLine;
    //方块的宽和高
    private float tvWidth, tvHeight;
    //比例为40
    private float sum = 40;
    //计算比例
    private float percent = 0.5f;
    //方块的y坐标
    private float pointY = 0f;
    //显示的价差
    private float price = 0f;
    //中间线
    private float lineHeight;
    //颜色集合
    private HashMap<Integer, String> colorMap = new HashMap<>();

    public TimeRateView(Context context) {
        this(context, null);
    }

    public TimeRateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeRateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        colorMap.put(0, "#2BD798");
        colorMap.put(1, "#000000");
        colorMap.put(2, "#FF6D74");

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintText.setStyle(Paint.Style.FILL);
        mPaintText.setColor(Color.WHITE);
        mPaintText.setTextSize(UIUtil.dp2px(10));
        mPaintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintLine.setStyle(Paint.Style.FILL);
        mPaintLine.setColor(Color.parseColor("#E2E2E2"));
        mPaintLine.setTextSize(UIUtil.dp2px(2));

        tvWidth = UIUtil.dp2px(35);
        tvHeight = UIUtil.dp2px(15);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        DrawLine(canvas);
        DrawTextBg(canvas);
    }

    private void DrawLine(Canvas canvas) {
        canvas.drawLine(tvWidth / 2, 0, tvWidth / 2, lineHeight, mPaintLine);
    }

    private void DrawTextBg(Canvas canvas) {

        RectF rectF = new RectF(0, pointY, tvWidth, pointY + tvHeight);
        canvas.drawRect(rectF, mPaint);

        //计算baseline
        Paint.FontMetrics fontMetrics = mPaintText.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = rectF.centerY() + distance;


//        String text = price + "";
        String text;
        if (price > 0) {
            text = "+" + price;
        } else {
            text = "" + price;
        }
        float width = mPaintText.measureText(text);
//        Log.e("TAG", rectF.width() + "rectF.width() === width:" + width);
        float x = (rectF.width() - width) / 2;
        canvas.drawText(text, x, baseline, mPaintText);
    }

    public void getPoint(float lossPrice, int flag, float lineSumHeight) {

        this.price = lossPrice;
        this.lineHeight = lineSumHeight;
        if (lossPrice - flag > 0) {
            mPaint.setColor(Color.parseColor(colorMap.get(0)));
            mPaint.setStyle(Paint.Style.FILL);
            mPaintText.setColor(Color.WHITE);
        } else if (lossPrice + flag < 0) {
            mPaint.setColor(Color.parseColor(colorMap.get(2)));
            mPaint.setStyle(Paint.Style.FILL);
            mPaintText.setColor(Color.WHITE);
        } else {
            mPaint.setColor(Color.parseColor(colorMap.get(1)));
            mPaint.setStyle(Paint.Style.STROKE);
            mPaintText.setColor(Color.BLACK);
        }
        lossPrice = lossPrice > 20 ? 20 : lossPrice;
        lossPrice = lossPrice < -20 ? -20 : lossPrice;
        lossPrice = Math.abs(lossPrice - 20);
        percent = lossPrice / sum;
        pointY = (lineSumHeight - tvHeight) * percent;
        invalidate();
    }
}
